package com.hikvision.open.app.ui.permission

import android.Manifest
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.FragmentActivity
import com.hikvision.open.app.utils.PermissionHelper
import com.hikvision.open.app.utils.inflate
import hik.sdk.demo.R
import hik.sdk.demo.databinding.ActivityMainBinding

/**
 * 权限申请演示 Activity
 */
class PermissionDemoActivity : AppCompatActivity() {
    
    private val viewBinding: ActivityMainBinding by inflate()
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(viewBinding.root)
        
        // 示例：申请联网权限（通常在 Android 6.0 及以上不需要动态申请）
        checkInternetPermission()
        
        // 示例：申请存储权限
        checkStoragePermission()
        
        // 示例：申请摄像头权限
        checkCameraPermission()
        
        // 示例：申请多个权限
        checkMultiplePermissions()
    }
    
    private fun checkInternetPermission() {
        // 联网权限通常在 Android 6.0 及以上不需要动态申请，但在 Manifest 中声明即可
        if (PermissionHelper.isGranted(this, Manifest.permission.INTERNET)) {
            // 已有联网权限
            Toast.makeText(this, "已获得联网权限", Toast.LENGTH_SHORT).show()
        }
    }
    
    private fun checkStoragePermission() {
        if (PermissionHelper.isGranted(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
            // 已有存储权限
            Toast.makeText(this, "已获得存储权限", Toast.LENGTH_SHORT).show()
        } else {
            // 请求存储权限
            PermissionHelper.requestStoragePermission(this) { allGranted ->
                if (allGranted) {
                    Toast.makeText(this, "存储权限已授予", Toast.LENGTH_SHORT).show()
                } else {
                    Toast.makeText(this, "存储权限被拒绝", Toast.LENGTH_SHORT).show()
                }
            }
        }
    }
    
    private fun checkCameraPermission() {
        if (PermissionHelper.isGranted(this, Manifest.permission.CAMERA)) {
            // 已有摄像头权限
            Toast.makeText(this, "已获得摄像头权限", Toast.LENGTH_SHORT).show()
        } else {
            // 请求摄像头权限
            PermissionHelper.requestCameraPermission(this) { granted ->
                if (granted) {
                    Toast.makeText(this, "摄像头权限已授予", Toast.LENGTH_SHORT).show()
                } else {
                    Toast.makeText(this, "摄像头权限被拒绝", Toast.LENGTH_SHORT).show()
                }
            }
        }
    }
    
    private fun checkMultiplePermissions() {
        val permissions = arrayOf(
            Manifest.permission.INTERNET,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.CAMERA
        )
        
        // 检查是否所有权限都已授予
        val allGranted = permissions.all { 
            PermissionHelper.isGranted(this, it) 
        }
        
        if (allGranted) {
            Toast.makeText(this, "所有权限均已获得", Toast.LENGTH_SHORT).show()
        } else {
            // 请求多个权限
            PermissionHelper.requestMultiplePermissions(
                this,
                permissions,
                "正常使用应用功能，需要您同意开启相关权限。",
                "部分权限未获得，将影响功能使用，请到设置中开启。"
            ) { resultMap ->
                val grantedPermissions = resultMap.filter { it.value }.keys
                val deniedPermissions = resultMap.filter { !it.value }.keys
                
                if (deniedPermissions.isEmpty()) {
                    Toast.makeText(this, "所有权限均已获得", Toast.LENGTH_SHORT).show()
                } else {
                    Toast.makeText(this, 
                        "已获得权限: ${grantedPermissions.size}, 被拒绝权限: ${deniedPermissions.size}", 
                        Toast.LENGTH_SHORT).show()
                }
            }
        }
    }
    
    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        PermissionHelper.handlePermissionsResult(requestCode, permissions, grantResults)
    }
}